﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using CH.Easy.Utility;
namespace CH.Wallpaper.Download
{
    public class ZolDownloader : Downloader
    {

        public override List<string> ResolutionList
        {
            get
            {
                return new List<string> { "1920x1200", "1920x1080", "1680x1050", "1600x900", "1440x900", "1366x768", "1280x1024", "1024x768" };
            }
        }

        public ZolDownloader()
        {

        }
      

        public override void Start()
        {
            ThreadPool.QueueUserWorkItem(delegate
            {

                ImageUrlList = new List<string>();
                string html = http.GetHtml(new HttpItem() { URL = this.URL });
                if (!string.IsNullOrEmpty(html))
                {
                    Regex r = new Regex(@"<img[^>]*[src|srcs]=.(?<src>[^>]*).{3}width\=.144[^>]*height\=.90.[^>]*?>");
                    MatchCollection mc = r.Matches(html);
                    foreach (Match m in mc)
                    {
                        ImageUrlList.Add(m.Groups["src"].Value.Replace("144x90", this.Resolution));
                    }
                }

                if (!Directory.Exists(this.SavePath))
                {
                    Directory.CreateDirectory(this.SavePath);
                }


                WebClient client = new WebClient();
                foreach (var src in ImageUrlList)
                {

                    try
                    {

                        client.DownloadFile(new Uri(src), SavePath + "\\" + index + src.GetExt());
                        index++;
                    }
                    catch (Exception)
                    {


                    }
                }

                base.Loader_ItemDownloadCompleteEvent("");
            });
        }

        public override bool IsAlbum(string url)
        {
            Regex r = new Regex(@"\/bizhi\/.*\.html");
            return r.IsMatch(url);
        }

        public override string FilterTitle(string title)
        {
            return title.Replace("-ZOL桌面壁纸", "").RegexReplace("高清桌面壁纸壁纸图片大全_太平电脑网壁纸库", "").Replace("【", "").Replace("】", "");
        }
    }
}
